﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LanAnh
{
    public partial class frmSearchKH : DevComponents.DotNetBar.Office2007Form
    {
        public frmSearchKH()
        {
            InitializeComponent();
        }
        Data data = new Data();
        public frmSearchKH(DataTable dtb)
        {
            InitializeComponent();            
        }        
        public delegate void PassData(String makh,string tenkh,string diachi, string dienthoai);
        public PassData passData;
        DataTable dt= new DataTable();
        private void frmSearchSP_Load(object sender, EventArgs e)
        {
            string sql = @"SELECT MaKH,TenKH,DienThoai,DiaChi FROM tblKhachHang";
            dt = data.executeQueryTable(sql);            
            cbeDMHanghoa.DataSource = dt;
            cbeDMHanghoa.ValueMember = "MaKH";
            cbeDMHanghoa.DisplayMember = "TenKH";            
        }
        string sprevious = string.Empty;
        private void cbeDMHanghoa_TextChanged(object sender, EventArgs e)
        {
            try
            {
                var ab = dt.Select("(TenKH like '*" + cbeDMHanghoa.Text.Replace("'", "''") + "*')");
                if (ab.Length > 0)
                {
                    GridViewdata.DataSource = ab.CopyToDataTable<DataRow>();
                    labelX3.Text = ab[0].Field<string>("TenKH");
                    btnOk.Tooltip = ab[0].Field<string>("MaKH");
                    lbDiaChi.Text = ab[0].Field<string>("DiaChi");
                    lbDienThoai.Text = ab[0].Field<string>("DienThoai");

                }
                else
                {
                    GridViewdata.DataSource = null;
                    labelX3.Text = string.Empty;
                    lbDiaChi.Text = string.Empty;
                    lbDienThoai.Text = string.Empty;
                    btnOk.Tooltip = string.Empty;
                }
            }
            catch { }
        }

        private void gridviewdm_CellClick(object sender, DataGridViewCellEventArgs e)
        {
          
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (!((labelX3.Text == string.Empty) || (labelX3.Text == "")))
            {
                //transfer(btnOk.Tooltip);
                passData(btnOk.Tooltip,labelX3.Text,lbDiaChi.Text ,lbDienThoai.Text);
                this.Close();
            }
            else
                MessageBox.Show("Vui lòng chọn 1 khách hàng ! ");
        }

        private void cbeDMHanghoa_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                GridViewdata.Focus();
            }
        }

        private void gridviewdm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnOk_Click(sender, e);
            }
        }

        private void gridviewdm_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                labelX3.Text = GridViewdata.Rows[e.RowIndex].Cells["TenKH"].Value.ToString();
                btnOk.Tooltip = GridViewdata.Rows[e.RowIndex].Cells["MaKH"].Value.ToString();
                lbDienThoai.Text = GridViewdata.Rows[e.RowIndex].Cells["DienThoai"].Value.ToString();
                lbDiaChi.Text = GridViewdata.Rows[e.RowIndex].Cells["DiaChi"].Value.ToString();

            }
        }
    }
}
